from CvPythonExtensions import *
import CvEventManager

import CustomFunctions
cf = CustomFunctions.CustomFunctions()

gc = CyGlobalContext()

class CvFungalBloom(CvEventManager.CvEventManager):
    def __init__(self):
        # initialize base class
        self.parent = CvEventManager.CvEventManager
        self.parent.__init__(self)

    def onCityDoTurn(self, argsList):
        pCity = argsList[0]
        iPlayer = argsList[1]

        pPlot = pCity.plot()
        pPlayer = gc.getPlayer(iPlayer)
        bPlayer = gc.getPlayer(gc.getBARBARIAN_PLAYER())

        ## Fungal Bloom

        if CyGame().getSorenRandNum(100, "FungalBloom") + pCity.ecoDamage() >= 100:
                iX = pPlot.getX()
                iY = pPlot.getY()
                listcitycircle8 = []
                for iiX in range(iX-1, iX+2, 1):
                        for iiY in range(iY-1, iY+2, 1):
                                pPlot2 = CyMap().plot(iiX,iiY)
                                listplot2circle8 = []
                                iX2 = pPlot2.getX()
                                iY2 = pPlot2.getY()
                                for iiX2 in range(iX2-1, iX2+2, 1):
                                        for iiY2 in range(iY2-1, iY2+2, 1):
                                                pPlot3 = CyMap().plot(iiX2,iiY2)
                                                if (pPlot3.getFeatureType() == gc.getInfoTypeForString('FEATURE_XENOFUNGUS') or pPlot3.getFeatureType() == gc.getInfoTypeForString('FEATURE_SEA_FUNGUS')):
                                                        listplot2circle8.append(pPlot3)
                                if len(listplot2circle8) > 0:
                                        if (pPlot2.getFeatureType() != gc.getInfoTypeForString('FEATURE_XENOFUNGUS') and pPlot2.getFeatureType() != gc.getInfoTypeForString('FEATURE_SEA_FUNGUS') and pPlot2.isCity() == False):
                                                listcitycircle8.append(pPlot2)
                if len(listcitycircle8) > 0:
                        iRnd = CyGame().getSorenRandNum(len(listcitycircle8), "Bloomplot")
                        pPlotBloom = listcitycircle8[iRnd]
                        pPlotBloom.setFeatureType(gc.getInfoTypeForString('FEATURE_XENOFUNGUS'), 0)
                        CyInterface().addMessage(pCity.getOwner(),True,25,'Fungal bloom around pCity.getName().','AS2D_DISCOVERBONUS',1,'Art/Interface/Buttons/Units/Diseasedcorpse.dds',ColorTypes(8),pPlotBloom.getX(),pPlotBloom.getY(),True,True)
                        pPlotNative = cf.PFFindClearPlotForMindWorm(-1, pPlotBloom)
                        if pPlotNative != -1:
                                newUnit = bPlayer.initUnit(gc.getInfoTypeForString('UNIT_MIND_WORM'), pPlotNative.getX(), pPlotNative.getY(), UnitAITypes.NO_UNITAI)
                else:
                        listcitycircle24 = []
                        for iiX in range(iX-2, iX+3, 1):
                                for iiY in range(iY-2, iY+3, 1):
                                        pPlot2 = CyMap().plot(iiX,iiY)
                                        listplot2circle8 = []
                                        iX2 = pPlot2.getX()
                                        iY2 = pPlot2.getY()
                                        for iiX2 in range(iX2-1, iX2+2, 1):
                                                for iiY2 in range(iY2-1, iY2+2, 1):
                                                        pPlot3 = CyMap().plot(iiX2,iiY2)
                                                        if (pPlot3.getFeatureType() == gc.getInfoTypeForString('FEATURE_XENOFUNGUS') or pPlot3.getFeatureType() == gc.getInfoTypeForString('FEATURE_SEA_FUNGUS')):
                                                                listplot2circle8.append(pPlot3)
                                        if len(listplot2circle8) > 0:
                                                if (pPlot2.getFeatureType() != gc.getInfoTypeForString('FEATURE_XENOFUNGUS') and pPlot2.getFeatureType() != gc.getInfoTypeForString('FEATURE_SEA_FUNGUS') and pPlot2.isCity() == False):
                                                        listcitycircle24.append(pPlot2)
                        if len(listcitycircle24) > 0:
                                iRnd = CyGame().getSorenRandNum(len(listcitycircle24), "Bloomplot")
                                pPlotBloom = listcitycircle24[iRnd]
                                pPlotBloom.setFeatureType(gc.getInfoTypeForString('FEATURE_XENOFUNGUS'), 0)
                                CyInterface().addMessage(pCity.getOwner(),True,25,'Fungal bloom around err... some base.','AS2D_DISCOVERBONUS',1,'Art/Interface/Buttons/Units/Diseasedcorpse.dds',ColorTypes(8),pPlotBloom.getX(),pPlotBloom.getY(),True,True)
                                pPlotNative = cf.PFFindClearPlotForMindWorm(-1, pPlotBloom)
                                if pPlotNative != -1:
                                        newUnit = bPlayer.initUnit(gc.getInfoTypeForString('UNIT_MIND_WORM'), pPlotNative.getX(), pPlotNative.getY(), UnitAITypes.NO_UNITAI)
                        else:
                                listcitycircle24 = []
                                for iiX in range(iX-2, iX+3, 1):
                                        for iiY in range(iY-2, iY+3, 1):
                                                pPlot2 = CyMap().plot(iiX,iiY)
                                                if (pPlot2.getFeatureType() != gc.getInfoTypeForString('FEATURE_XENOFUNGUS') and pPlot2.getFeatureType() != gc.getInfoTypeForString('FEATURE_SEA_FUNGUS') and pPlot2.isCity() == False):
                                                        listcitycircle24.append(pPlot2)
                                if len(listcitycircle24) > 0:
                                        iRnd = CyGame().getSorenRandNum(len(listcitycircle24), "Bloomplot")
                                        pPlotBloom = listcitycircle24[iRnd]
                                        pPlotBloom.setFeatureType(gc.getInfoTypeForString('FEATURE_XENOFUNGUS'), 0)
                                        CyInterface().addMessage(pCity.getOwner(),True,25,'Fungal bloom around err... some base.','AS2D_DISCOVERBONUS',1,'Art/Interface/Buttons/Units/Diseasedcorpse.dds',ColorTypes(8),pPlotBloom.getX(),pPlotBloom.getY(),True,True)
                                        pPlotNative = cf.PFFindClearPlotForMindWorm(-1, pPlotBloom)
                                        if pPlotNative != -1:
                                                newUnit = bPlayer.initUnit(gc.getInfoTypeForString('UNIT_MIND_WORM'), pPlotNative.getX(), pPlotNative.getY(), UnitAITypes.NO_UNITAI)
                                else:
                                        listcitycircle24 = []
                                        for iiX in range(iX-2, iX+3, 1):
                                                for iiY in range(iY-2, iY+3, 1):
                                                        pPlot2 = CyMap().plot(iiX,iiY)
                                                        if pPlot2.isCity() == False:
                                                                listcitycircle24.append(pPlot2)
                                                        iRnd = CyGame().getSorenRandNum(len(listcitycircle24), "Bloomplot")
                                                        pPlotBloom = listcitycircle24[iRnd]
                                                        pPlotNative = cf.PFFindClearPlotForMindWorm(-1, pPlotBloom)
                                                        if pPlotNative != -1:
                                                                newUnit = bPlayer.initUnit(gc.getInfoTypeForString('UNIT_MIND_WORM'), pPlotNative.getX(), pPlotNative.getY(), UnitAITypes.NO_UNITAI)
